# class Solution:
#     def lengthOfLongestSubstring(self, s):
#         T = []
#         max = 0
#         for i in range(len(s)):
#             if s[i] not in T:
#                 T.append(s[i])
#                 if len(T)>max:
#                     max = len(T)
#             else:
#                 f = T.index(s[i])
#                 T = T[f+1:]
#                 T.append(s[i])
#         return max


#官方解答
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        # 哈希集合，记录每个字符是否出现过
        occ = set()
        n = len(s)
        # 右指针，初始值为 -1，相当于我们在字符串的左边界的左侧，还没有开始移动
        rk, ans = -1, 0
        for i in range(n):
            if i != 0:
                # 左指针向右移动一格，移除一个字符
                occ.remove(s[i - 1])
            while rk + 1 < n and s[rk + 1] not in occ:
                # 不断地移动右指针
                occ.add(s[rk + 1])
                rk += 1
            # 第 i 到 rk 个字符是一个极长的无重复字符子串
            ans = max(ans, rk - i + 1)
        return ans


if __name__ == '__main__':
    T = Solution()
    F = T.lengthOfLongestSubstring("dvdf")
    print(F)